using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitTools;
namespace LitToolsTest
{
    public class ObjectPoolTest_Item : IObjectPoolItem
    {
        private GameObject gameObject;
        private Material _material;
        private Rigidbody _rigidbody;
        private const float LifeTime = 3f;
        private float bornTime;

        void IObjectPoolItem.OnRent(params object[] args)
        {
            InitCube((Color)args[0]);
            _rigidbody.WakeUp();
            gameObject.SetActive(true);
        }

        void IObjectPoolItem.OnRecycle()
        {
            _rigidbody.Sleep();
            gameObject.SetActive(false);
        }

        void IObjectPoolItem.OnCreate(params object[] args)
        {
            gameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
            _rigidbody = gameObject.AddComponent<Rigidbody>();
            _material =gameObject.GetComponent<MeshRenderer>().material;
            InitCube((Color)args[0]);
        }

        void IObjectPoolItem.OnRelease()
        {
            GameObject.Destroy(gameObject);
        }

        public void InitCube(Color color)
        {
            var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            pos.z = 0;
            gameObject.transform.position = pos;
            _material.color = color;
            bornTime = Time.realtimeSinceStartup;
        }

        public bool IsDeath()
        {
            return Time.realtimeSinceStartup - bornTime > LifeTime;
        }
    }
}